package com.junyidao.game.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.MsgDetailConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.mapper.TChannelMapper;
import com.junyidao.game.mapper.TGroupUserRelaMapper;
import com.junyidao.game.mapper.TMsgDetailMapper;
import com.junyidao.game.pojo.TMsgDetail;
import com.junyidao.game.vo.MsgSendedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/8/28.
 */
@Service
@Transactional
public class MsgDetailService {

    @Autowired
    private TMsgDetailMapper msgDetailMapper;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private TChannelMapper channelMapper;

    @Autowired
    private TGroupUserRelaMapper groupUserRelaMapper;

    public Map<String, Object> listMessages(MsgSendedVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getRows());
        vo.setUid(LoginInfo.getUser().getUid());
        List<MsgSendedVo> list = msgDetailMapper.getUserMessges(vo);
        PageInfo pageInfo = new PageInfo(list);
        Map<String, Object> ret = new HashMap<>();
        ret.put("list", list);
        ret.put("count", pageInfo.getTotal());
        return ret;
    }

    public void readMsgAll(String ids) {
        if (!StringUtils.isEmpty(ids)) {
            Map<String, Object> map = new HashMap<>();
            List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
            map.put("ids", idList);
            msgDetailMapper.readMsgAll(map);
        }
    }

    public void delMsgAll(String ids) {
        if (!StringUtils.isEmpty(ids)) {
            Map<String, Object> map = new HashMap<>();
            List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
            map.put("ids", idList);
            msgDetailMapper.delMsgAll(map);
        }
    }

    public void forwardMsg(String ids, String msgIds, String forwardType) {
        if (forwardType.equals("1")) {
            addUserMsg(ids, msgIds);
        } else if (forwardType.equals("2")) {
            addChannelMsg(ids, msgIds);
        } else {
            addGroupMsg(ids, msgIds);
        }

    }


    public void addUserMsg(String uids, String msgSendedId) {
        if (!StringUtils.isEmpty(uids)) {
            List<Integer> idList = JSONArray.parseArray(uids, Integer.class);
            if (idList.size() > 0) {
                List<Integer> msgList = JSONArray.parseArray(msgSendedId, Integer.class);
                for (Integer msgId : msgList) {
                    for (Integer uid : idList) {
                        Integer id = sequenceService.getNextSequence("MSG_DETAIL_ID");
                        TMsgDetail detail = new TMsgDetail();
                        detail.setMsgDetailId(id);
                        detail.setMsgStatus(BaseConstant.STATUS_NORMAL);
                        detail.setReadFlag(MsgDetailConstant.NOT_READ);
                        detail.setMsgSendedId(msgId);
                        detail.setMsgSource(MsgDetailConstant.MSG_SOURCE_USER);
                        detail.setObjectId(uid);
                        msgDetailMapper.insert(detail);
                    }
                }
            }
        }
    }

    public void addChannelMsg(String channelIds, String msgSendedId) {
        if (!StringUtils.isEmpty(channelIds)) {
            List<Integer> idList = JSONArray.parseArray(channelIds, Integer.class);
            if (idList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("ids", idList);
                List<Integer> uids = channelMapper.getUserIdsByChannelIds(map);
                List<Integer> msgList = JSONArray.parseArray(msgSendedId, Integer.class);
                for (Integer msgIds : msgList) {
                    for (Integer uid : uids) {
                        Integer id = sequenceService.getNextSequence("MSG_DETAIL_ID");
                        TMsgDetail detail = new TMsgDetail();
                        detail.setMsgDetailId(id);
                        detail.setMsgStatus(BaseConstant.STATUS_NORMAL);
                        detail.setReadFlag(MsgDetailConstant.NOT_READ);
                        detail.setMsgSendedId(msgIds);
                        detail.setMsgSource(MsgDetailConstant.MSG_SOURCE_CHANNEL);
                        detail.setObjectId(uid);
                        msgDetailMapper.insert(detail);
                    }
                }

            }
        }
    }

    public void addGroupMsg(String groupIds, String msgSendedId) {
        if (!StringUtils.isEmpty(groupIds)) {
            List<Integer> idList = JSONArray.parseArray(groupIds, Integer.class);
            if (idList.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("ids", idList);
                List<Integer> uids = groupUserRelaMapper.getUidsByGroupIds(map);
                List<Integer> msgList = JSONArray.parseArray(msgSendedId, Integer.class);
                for (Integer msgId : msgList) {
                    for (Integer uid : uids) {
                        Integer id = sequenceService.getNextSequence("MSG_DETAIL_ID");
                        TMsgDetail detail = new TMsgDetail();
                        detail.setMsgDetailId(id);
                        detail.setMsgStatus(BaseConstant.STATUS_NORMAL);
                        detail.setReadFlag(MsgDetailConstant.NOT_READ);
                        detail.setMsgSendedId(msgId);
                        detail.setMsgSource(MsgDetailConstant.MSG_SOURCE_GROUP);
                        detail.setObjectId(uid);
                        msgDetailMapper.insert(detail);
                    }
                }

            }
        }
    }

}
